package com.example.sharesinfo.service;

import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.IBinder;
import android.view.SurfaceView;

import com.example.sharesinfo.bean.TextBean;
import com.example.sharesinfo.bean.UserBean;
import com.example.sharesinfo.entity.Constants;
import com.example.sharesinfo.event.EventManager;
import com.example.sharesinfo.event.MessageEvent;
import com.example.sharesinfo.helper.GlideHelper;
import com.example.sharesinfo.manager.CloudManager;
import com.example.sharesinfo.ui.ChatActivity;
import com.example.sharesinfo.utils.LogUtils;
import com.example.sharesinfo.utils.SpUtils;
import com.google.gson.Gson;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Message;
import io.rong.message.TextMessage;


public class CloudService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        initService();
        linkCloudServer();
    }

    /**
     * 连接云服务
     */
    private void linkCloudServer() {
        //获取Token
        String token = SpUtils.getInstance().getString(Constants.SP_TOKEN, "");
        LogUtils.e("token:" + token);
        //连接服务
        CloudManager.getInstance().connect(token);
        //接收消息
        CloudManager.getInstance().setOnReceiveMessageListener(new RongIMClient.OnReceiveMessageListener() {
            @Override
            public boolean onReceived(Message message, int i) {
                parsingImMessage(message);
                return false;
            }
        });

    }

    private void initService() {
        EventManager.register(this);
    }

    /**
     * 解析消息体
     *
     * @param message
     */
    private void parsingImMessage(final Message message) {
        String objectName = message.getObjectName();
        if (objectName.equals(CloudManager.MSG_TEXT_NAME)) {
            //获取消息主体
            TextMessage textMessage = (TextMessage) message.getContent();
            String content = textMessage.getContent();
            LogUtils.i("content:" + content);
            TextBean textBean = null;
            try {
                LogUtils.i("Gson Try:1");
                textBean = new Gson().fromJson(content, TextBean.class);
                LogUtils.i("Gson Try:2");
            } catch (Exception e) {
                LogUtils.i("Gson Try:" + e.toString());
                e.printStackTrace();
            }
            LogUtils.i("Gson Try:3");
            if (null == textBean) {
                LogUtils.i("Gson Try:4");
                //系统调试消息
                MessageEvent event = new MessageEvent(EventManager.FLAG_SEND_TEXT);
                event.setText(content);
                event.setUserId(message.getSenderUserId());
                EventManager.post(event);
                //pushSystem(message.getSenderUserId(), 1, 0, 0, content);
                return;
            }
            //普通消息
            if (textBean.getType().equals(CloudManager.TYPE_TEXT)) {
                MessageEvent event = new MessageEvent(EventManager.FLAG_SEND_TEXT);
                event.setText(textBean.getMsg());
                event.setUserId(message.getSenderUserId());
                EventManager.post(event);
                //pushSystem(message.getSenderUserId(), 1, 0, 0, textBean.getMsg());
            }
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        switch (event.getType()) {
            case EventManager.FLAG_SEND_CAMERA_VIEW:
                SurfaceView sv = event.getmSurfaceView();
                if (sv != null) {
                    //mRemoteView = sv;
                }
                //updateVideoView();
                break;
        }
    }

}
